package com.kun.hosp.controller.threeapi;

import cn.hutool.core.util.StrUtil;
import com.kun.commonutil.exception.HospitalException;
import com.kun.commonutil.hosputil.HttpRequestHelper;
import com.kun.commonutil.result.Result;
import com.kun.commonutil.result.ResultCodeEnum;
import com.kun.hosp.service.DepartmentService;
import com.kun.hosp.service.HospitalService;
import com.kun.hosp.service.ScheduleService;
import com.kun.model.entity.hosp.Department;
import com.kun.model.entity.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author zhoukun
 * @Date 2021/5/1 17:53
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiHospController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        hospitalService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        //必须参数校验
        String hoscode = (String)paramMap.get("hoscode");
        if(StrUtil.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }

        //查询
        return Result.ok(hospitalService.getByHoscode((String)paramMap.get("hoscode")));
    }
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StrUtil.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        departmentService.saveDepartmen(paramMap);
        return Result.ok();
    }
    @ApiOperation(value = "科室查询获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StrUtil.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Department> pageModel = departmentService.selectPage(paramMap);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        //必须参数校验
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String depcode = (String)paramMap.get("depcode");
        if(StrUtil.isEmpty(hoscode)&&StrUtil.isEmpty(depcode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        departmentService.remove(depcode);
        return Result.ok();
    }
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        String hoscode = (String)paramMap.get("hoscode");
        if(StrUtil.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StrUtil.isEmpty(hoscode)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Schedule> pageModel = scheduleService.selectPage(paramMap);
        return Result.ok(pageModel);
    }
    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //签名校验
        hospitalService.HasSignKey(paramMap);
        //必须参数校验
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        if(StrUtil.isEmpty(hoscode)&&StrUtil.isEmpty(hosScheduleId)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        scheduleService.remove(hosScheduleId );
        return Result.ok();
    }

}
